package cn.tansanqinger.dao.sql.comment;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ICommentusertopicSqlDao;
import cn.tansanqinger.pojo.Commentusertopic;

@Repository
public class CommentusertopicSqlDaoImpl {

	@Autowired
	private ICommentusertopicSqlDao dao;
	
	/**
	 * 返回一定数量的被点赞的数据
	 * @param uid
	 * @param page
	 * @return
	 */
	public List<Commentusertopic> replyToComment(int uid, int page){
		QueryWrapper<Commentusertopic> queryWrapper = new QueryWrapper<Commentusertopic>();
		queryWrapper.lambda().eq(Commentusertopic::getWuid, uid);
		Page<Commentusertopic> pg = new Page<Commentusertopic>(page,10);
		IPage<Commentusertopic> iPage = dao.selectPage(pg, queryWrapper);
		List<Commentusertopic> commentusertopics = iPage.getRecords();
		for (int i = 0; i < commentusertopics.size(); i++) {//将查看的数据数据标记未查看过
			Commentusertopic commentusertopic = commentusertopics.get(i);
			if(commentusertopic.getIsselect()==0) {
				commentusertopic.setIsselect(1);
				modification(commentusertopic);
			}
		}
		return commentusertopics;
	}
	
	/**
	 * 查询数据被点赞的总数
	 * @param uid
	 * @return
	 */
	public int praiseCountAll(int uid) {
		QueryWrapper<Commentusertopic> queryWrapper = new QueryWrapper<Commentusertopic>();
		queryWrapper.lambda().eq(Commentusertopic::getWuid, uid).eq(Commentusertopic::getIsdelete, 0);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 查询点赞的数据未被查看的信息
	 * @param uid
	 * @return
	 */
	public int praiseCountIsSelect(int uid) {
		QueryWrapper<Commentusertopic> queryWrapper = new QueryWrapper<Commentusertopic>();
		queryWrapper.lambda().eq(Commentusertopic::getWuid, uid).eq(Commentusertopic::getIsdelete, 0).eq(Commentusertopic::getIsselect, 0);
		return dao.selectCount(queryWrapper);
	}
	
	/**
	 * 根据条件查询
	 * @param cid
	 * @param uid
	 * @return
	 */
	public Commentusertopic selectTidUidOne(int cid, int uid) {
		QueryWrapper<Commentusertopic> queryWrapper = new QueryWrapper<Commentusertopic>();
		queryWrapper.lambda().eq(Commentusertopic::getUid, uid).eq(Commentusertopic::getCid, cid);
		return dao.selectOne(queryWrapper);
	}
	
	/**
	 * 根据主键查询数据
	 * @param cuid
	 * @return
	 */
	public Commentusertopic selectCuidOne(int cuid) {
		return dao.selectById(cuid);
	}
	
	/**
	 * 添加数据
	 * @param commentusertopic
	 * @return
	 */
	public int add(Commentusertopic commentusertopic) {
		return dao.insert(commentusertopic);
	}
	
	/**
	 * 修改数据
	 * @param commentusertopic
	 * @return
	 */
	public int modification(Commentusertopic commentusertopic) {
		return dao.updateById(commentusertopic);
	}
	
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	public int delete(int id) {
		return dao.deleteById(id);
	}
}
